package com.rdc.example.controller;

import com.rdc.example.bean.Result;
import com.rdc.example.bean.dto.ArticleDTO;
import com.rdc.example.service.impl.LikeServiceImpl;
import com.rdc.example.view.ArticleView;

import java.util.List;

import static com.rdc.example.controller.UserController.utilView;

/**
 * @author duck
 */
public class LikeController {
    static LikeServiceImpl likeService = new LikeServiceImpl();

    public static void turnLike(Integer userId, Integer articleId) throws Exception {
        try {
            if (userId == null) {
                utilView.showError("userId is null");
                return;
            }
            if (articleId == null) {
                utilView.showError("articleId is null");
                return;
            }
            Result result= likeService.turnLike(userId, articleId);
            utilView.showMessage(result.getMessage());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void showLikeArticleList(Integer userId) throws Exception {
        try {
            if (userId == null) {
                utilView.showError("userId is null");
            }
            List<ArticleDTO> articleList = likeService.getLikeArticlesListByUserId(userId);
            if (articleList == null || articleList.isEmpty()) {
                utilView.showError("没有点赞过的文章哦");
                return;
            }
            for(int i=0;i<articleList.size();i++){
                ArticleView.showArticle(i, articleList.get(i));
            }
            MainController.currentArticleDTOList = articleList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    };



}
